package com.lyf.scm.plan.api.controller;

import com.lyf.scm.common.model.ExcelExportDataDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.lyf.scm.plan.api.dto.GmvTargetDTO;
import com.lyf.scm.plan.domain.service.GmvTargetService;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * description:
 * gmv指标Controller
 *
 * @author: haodong
 * @data: 2020/7/8 16:20
 */
@Slf4j
@Api(tags = "gmv指标")
@RequestMapping("/api/v1/gmvTarget")
@RomeController
public class GmvTargetController {

    @Resource
    private GmvTargetService gmvTargetService;


    @PostMapping("/list")
    @ApiOperation(value = "按条件查询gmv指标(周)")
    public Response listBySelective(@RequestBody GmvTargetDTO gmvTarget) {
        return Response.builderSuccess(gmvTargetService.listBySelective(gmvTarget));
    }


    @PostMapping("/listAll")
    @ApiOperation(value = "查询全渠道gmv指标(范围)")
    public Response listAllByCycles(@RequestBody GmvTargetDTO gmvTarget) {
        return Response.builderSuccess(gmvTargetService.listAllByCycles(gmvTarget));
    }

    @PostMapping("batchSave")
    @ApiOperation(value = "批量新增")
    public Response batchSave(@RequestBody List<GmvTargetDTO> gmvTargetList) {
        return Response.builderSuccess(gmvTargetService.batchSave(gmvTargetList));
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void exportExcel(@RequestBody GmvTargetDTO gmvTargetDTO, @RequestParam("type") Integer type, HttpServletResponse httpServletResponse) {
        try {
            ExcelExportDataDTO excelExportDataDTO = gmvTargetService.exportExcel(gmvTargetDTO, type);
            ExcelExportUtil.exportExcel(httpServletResponse, excelExportDataDTO);
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }

    @PostMapping("/updateTarget")
    @ApiOperation("更新销售额目标")
    private Response updateTarget(@RequestBody List<GmvTargetDTO> gmvTargetDTO){
        return Response.builderSuccess(gmvTargetService.updateTarget(gmvTargetDTO));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除对应标识数据")
    public Response delete(@RequestParam("opSign") String opSign) {
        gmvTargetService.delete(opSign);
        return Response.builderSuccess(null);
    }
}
